var express = require('express');
var router = express.Router();
var API = require('../utils/mysql.js');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });
});

// 查询数据
router.get('/list', function (req, res, next) {
  var Api = new API(req, res, next);
  Api.get('content').then((data) => {
    res.json({
      code: 200,
      data: data
    });
  });
});

// 增加数据
router.get('/add', function (req, res, next) {
  var Api = new API(req, res, next);
  Api.add('content', {
    content: 'content',
    title: 'title',
  }).then((data) => {
    res.json({
      code: 200,
      data: data
    });
  });
});

// 删除数据
router.get('/delete', function (req, res, next) {
  var Api = new API(req, res, next);
  Api.delete('content', 'id = 7').then((data) => {
    res.json({
      code: 200,
      data: data
    });
  });
});

// 编辑数据
router.get('/edit', function (req, res, next) {
  var Api = new API(req, res, next);
  Api.edit('content', {
    content: '修改后的content',
    title: '修改后的title',
  },'id = 8').then((data) => {
    res.json({
      code: 200,
      data: data
    });
  });
});

router.post('/upload', function(req, res, next) {
  console.log(req);
  res.end('执行结束');
});

module.exports = router;